package com.avic.rong.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.media.ExifInterface;
import android.util.Log;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class BitmapUtil {
    public static int max = 0;
    public static boolean act_bool = true;
    public static List<Bitmap> bmp = new ArrayList<Bitmap>();

    public static List<String> drr = new ArrayList<String>();

    public static Bitmap revitionImageSize(String path) throws IOException {
        BufferedInputStream in = new BufferedInputStream(new FileInputStream(
                new File(path)));
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(in, null, options);
        in.close();
        int i = 0;
        Bitmap bitmap = null;
        while (true) {
            if ((options.outWidth >> i <= 1000)
                    && (options.outHeight >> i <= 1000)) {
                in = new BufferedInputStream(
                        new FileInputStream(new File(path)));
                options.inSampleSize = (int) Math.pow(2.0D, i);
                options.inJustDecodeBounds = false;
                bitmap = BitmapFactory.decodeStream(in, null, options);
                break;
            }
            i += 1;
        }

        bitmap = rotaingBitmap(bitmap, path);
        return bitmap;
    }

    /**
     * 读取图片属性：旋转的角度
     * 
     * @param path
     *            图片绝对路径
     * @return degree旋转的角度
     */
    public static int readPictureDegree(String path) {
        int degree = 0;
        try {
            ExifInterface exifInterface = new ExifInterface(path);
            int orientation = exifInterface.getAttributeInt(
                    ExifInterface.TAG_ORIENTATION,
                    ExifInterface.ORIENTATION_NORMAL);
            switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    degree = 90;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    degree = 180;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    degree = 270;
                    break;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return degree;
    }

    /*
     * 旋转图片
     * 
     * @param angle
     * 
     * @param bitmap
     * 
     * @return Bitmap
     */
    public static Bitmap rotaingBitmap(Bitmap bitmap, String path) {
        // 旋转图片 动作
        int degree = readPictureDegree(path);
        Matrix matrix = new Matrix();
        ;
        matrix.postRotate(degree);
        Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,
                bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        // if (!bitmap.isRecycled()) {
        // bitmap.recycle();
        // }
        return resizedBitmap;
    }

    public static Bitmap cutBitmap(Bitmap bitmap, Rect bounds) {
    	float imageViewWidth = bounds.right - bounds.left;
    	float imageViewHeight = bounds.bottom - bounds.top;
    	return cutBitmap(bitmap, imageViewWidth, imageViewHeight);
	}
        
    public static Bitmap cutBitmap(Bitmap bitmap, float imageViewWidth, float imageViewHeight) {
		try {
			float width = bitmap.getWidth();
			float height = bitmap.getHeight();
			float toWidth = 0;
			float toHeight = 0;
			Bitmap newBitmap;
			if (width >= height) {
				if ((width/height) >= (imageViewWidth/imageViewHeight)) {
					toHeight = height;
					toWidth = ((float)(toHeight * imageViewWidth)) / ((float)imageViewHeight);
					newBitmap = Bitmap.createBitmap(bitmap, (int)((width - toWidth)/2), 0, (int)toWidth, (int)toHeight);
				} else {
					toWidth = width;
					toHeight = (toWidth * imageViewHeight)/imageViewWidth;
					newBitmap = Bitmap.createBitmap(bitmap, 0, (int)((height - toHeight)/2), (int)toWidth, (int)toHeight);
				}
			} else {
				if ((width/height) >= (imageViewWidth/imageViewHeight)) {
					toHeight = height;
					toWidth = ((float)(toHeight * imageViewWidth)) / ((float)imageViewHeight);
					newBitmap = Bitmap.createBitmap(bitmap, (int)((width - toWidth)/2), 0, (int)toWidth, (int)toHeight);
				} else {
					toWidth = width;
					toHeight = (toWidth * imageViewHeight)/imageViewWidth;
					newBitmap = Bitmap.createBitmap(bitmap, 0, 0, (int)toWidth, (int)toHeight);
				}
			}
			return newBitmap;
		} catch (Exception e) {
			Log.e("exception ", e.toString());
			return null;
		}
	}
}
